const Controller = require("uni-cloud-router").Controller;
const {validateToken} = require('validdate-token');
module.exports = class UserController extends Controller{
	/**
	 * @param {Object} token
	 * 验证token
	 */
	async _checkToken(token){
		let tokenRes;
		try{
			tokenRes = await validateToken(token);
			if(tokenRes.code !== 0){
				return{
					status:-1,
					msg:"稍后重试",
					errCode:tokenRes.errCode
				}
			} else {
				return {
					status:0,
					msg:"token有效",
				}
			}
		}catch(e){
			return {
			  status: -1,
			  errCode: 'TOKEN_INVALID',
			  msg: 'token无效'
			}
		}
	}
	/**
	 * 2020-11-22
	 * 获取员工历史签到 信息
	 */
	async getHistoryProject(){
		let {token,wkID,pageNo,pageSize} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.wkSignAdmin.getHistoryProject({wkID,pageNo,pageSize});
		}
	}
	/**
	 * 2020-11-23
	 * 员工 筛选自己的打卡记录
	 */
	async filtrateSign(){
		let {wkID,token,curYearMonth} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.wkSignAdmin.filtrateSign({wkID,curYearMonth});
		}
	}
	/**
	 * 2020-11-22
	 * 员工获取项目 | 进行中 | 历史项目
	 */
	async getUnderwayProject(){
		let {token,wkID,projectStatus} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.wkSignAdmin.getUnderwayProject({wkID,projectStatus});
		}
	}
	/**
	 * 2020-11-23
	 * 工人打卡
	 */
	async signin(){
		let {token,data} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.wkSignAdmin.signin({data});
		}
	}
}